<?php
/**********************************************************************
    Copyright (C) FrontAccounting, LLC.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************/
$page_security = 'SA_SUPPTRANSVIEW';
// ----------------------------------------------------------------
// $ Revision:	2.0 $
// Creator:	Joe Hunt
// date_:	2005-05-19
// Title:	Sales Summary Report
// ----------------------------------------------------------------
$path_to_root="..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/gl/includes/gl_db.inc");
include_once($path_to_root . "/includes/db_pager.inc");
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/purchasing/includes/purchasing_ui.inc");
include_once($path_to_root . "/reporting/includes/reporting.inc");

//------------------------------------------------------------------


print_pembelian_perhari_report();

//function getTaxTransactions($from, $to, $tax_id)
function getTaxTransactions($from, $to)
{
	$fromdate = date2sql($from);
	$todate = date2sql($to);

/* 	 $sql = "SELECT ddebtor_no, d.name AS cust_name,  dt.type, dt.trans_no,  
			ov_amount AS total,dt.reference
		FROM ".TB_PREF."debtor_trans dt
			LEFT JOIN ".TB_PREF."debtors_master d ON d.debtor_no=dt.debtor_no
		WHERE dt.type=".ST_CUSTPAYMENT." AND dt.tran_date >=".db_escape($fromdate)." AND dt.tran_date<=".db_escape($todate)."
		ORDER BY d.debtor_no"; */
	 
	$sql=" 
	SELECT trans.type
     , trans.trans_no
     , trans.reference
     , supplier.supp_name
     , trans.supp_reference
     , trans.tran_date
     , trans.due_date
     , supplier.curr_code
     , (trans.ov_amount + trans.ov_gst + trans.ov_discount) AS TotalAmount
     , trans.alloc AS Allocated
     , ((trans.type = 20 OR trans.type = 21) AND trans.due_date < '2012-02-02') AS OverDue
     , (abs(trans.ov_amount + trans.ov_gst + trans.ov_discount - trans.alloc) <= 0.005) AS Settled
	FROM
	  supp_trans AS trans, suppliers AS supplier
	WHERE
	  (supplier.supplier_id = trans.supplier_id
	  AND trans.tran_date >= ".db_escape($fromdate)."
	  AND trans.tran_date <= ".db_escape($todate)."
	  AND trans.ov_amount != 0
	  AND trans.supplier_id = '1')
	ORDER BY
  tran_date DESC
	";
    return db_query($sql,"No transactions were returned");
}

/*function getTaxes($type, $trans_no)
{
	$sql = "SELECT included_in_price, SUM(CASE WHEN trans_type=".ST_CUSTCREDIT." THEN -amount ELSE amount END * ex_rate) AS tax
		FROM ".TB_PREF."trans_tax_details WHERE trans_type=$type AND trans_no=$trans_no GROUP BY included_in_price";

    $result = db_query($sql,"No transactions were returned");
    if ($result !== false)
    	return db_fetch($result);
    else
    	return null;
} */   	

//----------------------------------------------------------------------------------------------------

function print_pembelian_perhari_report()
{
	global $path_to_root;
	
	$from = $_POST['PARAM_0'];
	$to = $_POST['PARAM_1'];
/*	$tax_id = $_POST['PARAM_2'];
	$comments = $_POST['PARAM_3'];*/
	$destination = $_POST['PARAM_4'];
/*	if ($tax_id == 0)
		$tid = _('No');
	else
		$tid = _('Yes');
*/

	if ($destination)
		include_once($path_to_root . "/reporting/includes/excel_report.inc");
	else
		include_once($path_to_root . "/reporting/includes/pdf_report.inc");

	$dec = user_price_dec();

	$rep = new FrontReport(_('Laporan Pembelian Harian'), "Laporan Pembelian Harian", user_pagesize());

	/*$params =   array( 	0 => $comments,
						1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
						2 => array(  'text' => _('Tax Id Only'),'from' => $tid,'to' => ''));
*/
	$cols = array(0, 20, 100, 200, 300, 500);

	$headers = array('No', 'No. Pembelian', 'Supplier','Tanggal Transaksi', 'Total');
	$aligns = array('left', 'left', 'left','left','right');
	$rep->Font();
	$rep->Info($params, $cols, $headers, $aligns);
	$rep->NewPage();
	
	$totalnet = 0.0;
	$totaltax = 0.0;
	//$transactions = getTaxTransactions($from, $to, $tax_id);
    $transactions = getTaxTransactions($from, $to);

	//$rep->TextCol(0, 4, _('Balances in Home Currency'));
	$rep->NewLine(0);
	
	$custno = 0;
	$tax = $total = 0;
	$i = 0;
	$custname = ""; //$tax_id =
	while ($trans=db_fetch($transactions))
	{$i++; 
		    $nofaktur = $trans['reference'];
			$supname = $trans['supp_name'];
			$total = $trans['TotalAmount'];
		//if ($custno != $trans['debtor_no'])
		//{
			//if ($custno != 0)
			//{
				$rep->TextCol(0, 1, $i);
		        $rep->TextCol(1, 2, $nofaktur);
		        $rep->TextCol(2, 3, $supname);
		        $rep->DateCol(3, 4,	$trans['tran_date'], true);		        
		       	$rep->AmountCol(4, 5, $total, $dec);
						
				$totalnet += $total;
				$total = $tax = 0;
				$rep->NewLine();

				if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
				{
					$rep->Line($rep->row - 2);
					$rep->NewPage();
				}
			//}

		
	}

	$rep->Font('bold');
	$rep->NewLine();
	$rep->Line($rep->row + $rep->lineHeight);
	$rep->TextCol(0, 2,	_("Total Pembelian"));
	$rep->AmountCol(4, 5, $totalnet, $dec);
	//$rep->AmountCol(3, 4, $totaltax, $dec);
	$rep->Line($rep->row - 5);
	$rep->Font();
	$rep->End();
}

?>